//
//  CXToolBarView.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/22.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit

class CXToolBarView: UIView {
    
    //换一种方式来创建子控件
    var repostBtn: UIButton!
    var commentBtn: UIButton!
    var attributeBtn: UIButton!
    var splitView1: UIView!
    var splitView2: UIView!
    
    var statusViewModel: CXStatusViewModel? {
        
        didSet {
            //设置三个按钮的内容
            repostBtn.setTitle(statusViewModel?.repostButtonText, forState: .Normal)
            commentBtn.setTitle(statusViewModel?.commentButtonText, forState: .Normal)
            attributeBtn.setTitle(statusViewModel?.attributeButtonText, forState: .Normal)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CXToolBarView {
    
    private func setupUI() {
        
        repostBtn = addButton("转发", imageNamed: "timeline_icon_retweet")
        commentBtn = addButton("评论", imageNamed: "timeline_icon_comment")
        attributeBtn = addButton("赞", imageNamed: "timeline_icon_unlike")
        splitView1 = addSplitLine()
        splitView2 = addSplitLine()
        
        //设置约束
        repostBtn.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentBtn)
        }
        commentBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(repostBtn.snp_trailing)
            make.top.bottom.equalTo(repostBtn)
            make.width.equalTo(attributeBtn)
        }
        attributeBtn.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(commentBtn)
            make.leading.equalTo(commentBtn.snp_trailing)
            make.trailing.equalTo(self)
        }
        splitView1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(repostBtn.snp_trailing)
            make.centerY.equalTo(repostBtn)
        }
        splitView2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentBtn.snp_trailing)
            make.centerY.equalTo(commentBtn)
        }
    }
    //添加按钮
    private func addButton(title: String, imageNamed: String) -> UIButton {
        
        let button = UIButton()
        button.setImage(UIImage(named: imageNamed), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Highlighted)
        
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
       // button.sizeToFit()  //自动调整按钮大小
        
        addSubview(button)
        return button
    }
    //添加分割线
    private func addSplitLine() -> UIView {
        let line = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(line)
        return line
    }
}